package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.domain.dto.BusinessDto;
import com.crm.domain.entity.Business;
import com.crm.domain.vo.BusinessVo;
import com.crm.mapper.BusinessMapper;
import com.crm.service.BusinessService;
import com.crm.utils.HttpStatus;
import com.crm.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService{
    private final BusinessMapper businessMapper;

    /**
     * 分页查询业务信息
     *
     * @param businessDto
     * @return
     */
    @Override
    public PageResult<BusinessVo> queryAll(BusinessDto businessDto) {
        //设置分页参数
        Page<Business> page = Page.of(businessDto.getPageNum(), businessDto.getPageSize());
        //调用mapper查询数据
        LambdaQueryWrapper<Business> wrapper = Wrappers.<Business>lambdaQuery()
                .eq(ObjectUtils.isNotEmpty(businessDto.getId()), Business::getId, businessDto.getId())
                .like(ObjectUtils.isNotEmpty(businessDto.getName()), Business::getName, businessDto.getName())
                .like(ObjectUtils.isNotEmpty(businessDto.getPhone()), Business::getPhone, businessDto.getPhone())
                .eq(ObjectUtils.isNotEmpty(businessDto.getOwner()), Business::getOwner, businessDto.getOwner())
                .eq(ObjectUtils.isNotEmpty(businessDto.getStatus()), Business::getStatus, businessDto.getStatus())
                .ge(ObjectUtil.isNotEmpty(businessDto.getBeginCreateTime()), Business::getBeginCreateTime, businessDto.getBeginCreateTime())
                .le(ObjectUtil.isNotEmpty(businessDto.getEndCreateTime()), Business::getEndCreateTime, businessDto.getEndCreateTime());

        //用mybatis查询
        Page<Business> businessPage = businessMapper.selectPage(page, wrapper);
        //将mybatis查询结果转换为自定义对象
        // 转换结果
        List<BusinessVo> businessVoList = businessPage.getRecords().stream()
                .map(business -> BeanUtil.copyProperties(business, BusinessVo.class))
                .collect(Collectors.toList());
        // 构建分页响应
        PageResult<BusinessVo> pageResult = new PageResult<>();
        pageResult.setTotal(businessVoList.size());
        pageResult.setRows(businessVoList);
        pageResult.setCode(HttpStatus.SUCCESS);
        pageResult.setMsg("查询成功");
        return pageResult;
    }

    /**
     * 新增用户信息
     * @param business
     */
    @Override
    public void add(@RequestBody Business business) {
        //新增用户信息
        businessMapper.insert(business);
    }
    /**
     * 判断用户是否存在
     * @param phone
     * @return
     */
    @Override
    public boolean checkIfExistsByPhone(String phone) {
        return businessMapper.selectOne(Wrappers.<Business>lambdaQuery().eq(Business::getPhone, phone))!= null;
    }

}


